package com.lmy.animation

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.Animation
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    lateinit var ivIcon: ImageView
    lateinit var bt01: Button
    lateinit var bt02: Button
    lateinit var bt03: Button
    lateinit var btclose: Button
    lateinit var animatorStartView: ObjectAnimator
    lateinit var animatorStartView2: ObjectAnimator
    lateinit var animatorStartView3: ObjectAnimator
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initData()
        bt01.setOnClickListener {
            animatorStartView.apply {
                duration = 1000
                repeatCount = Animation.INFINITE//动画循环执行
                start()
            }
        }
        bt02.setOnClickListener {
            animatorStartView2.apply {
                duration = 2000
                repeatCount = Animation.INFINITE//动画循环执行
                start()
            }
        }

        bt03.setOnClickListener {
            animatorStartView3.apply {
                duration = 2000
                start()
            }
        }
        btclose.setOnClickListener {
            animatorStartView.cancel()
            ivIcon.apply {
                scaleX = 1f
                scaleY = 1f
            }
            animatorStartView2.cancel()
        }
    }


    private fun initData() {
        //不改变大小属性动画
        animatorStartView = ObjectAnimator.ofPropertyValuesHolder(
            ivIcon,
            PropertyValuesHolder.ofFloat("scaleY", 1f, 1.25f, 1f),
            PropertyValuesHolder.ofFloat("scaleX", 1f, 1.25f, 1f)
        )

        //改变大小的属性动画
        var wrapperView = WrapperView(ivIcon)
        animatorStartView2 = ObjectAnimator.ofPropertyValuesHolder(
            wrapperView,
            PropertyValuesHolder.ofInt("width", 50, 500, 50),
            PropertyValuesHolder.ofInt("height", 50, 500, 50)
//
        )

        animatorStartView3 = ObjectAnimator.ofPropertyValuesHolder(
            ivIcon,
            PropertyValuesHolder.ofFloat("translationX", 0f, 300f, 0f),
        )
    }

    private fun initView() {
        ivIcon = findViewById<ImageView>(R.id.iv_icon)
        bt01 = findViewById<Button>(R.id.bt_01)
        bt02 = findViewById<Button>(R.id.bt_02)
        bt03 = findViewById<Button>(R.id.bt_03)
        btclose = findViewById<Button>(R.id.bt_close)
    }

    @SuppressLint("ObjectAnimatorBinding")
    class WrapperView(var mtarget: View) {
        var width: Int
            get() = mtarget.layoutParams.width
            set(width) {
                mtarget.layoutParams.width = width
                mtarget.requestLayout()
            }
        var height: Int
            get() = mtarget.layoutParams.height
            set(height) {
                mtarget.layoutParams.height = height
                mtarget.requestLayout()
            }
    }
}







